<?php
/**
 * (model) 备货日期设置
 */
namespace App\Models\Stocking;

use App\Models\Basics\SysArea;
use App\Models\Product\ProductAmazonSku;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Models\SsBasic\AreaSku;
use Illuminate\Database\Eloquent\Model;
class StockingSetDate extends Model
{
    protected $table = 's_stocking_set_date';

    protected $guarded = [];

    public function area(){
        return $this->belongsTo(SysArea::class,'area_id');
    }

    public function classify(){
        return $this->belongsTo(ProductClassify::class,'classify_id');
    }

    const STATUS_ERROR = 0; //异常
    const STATUS_NORMAL = 1; //正常
    public static $_status = [
        self::STATUS_ERROR => '异常',
        self::STATUS_NORMAL => '正常'
    ];

    /**
     * 添加备货日期
     * @param $areaId
     * @param $classifyId
     * @param $pCycle
     * @param $seaDay
     */
    public static function addSetDate($areaId,$classifyId,$pCycle,$seaDay){
        $seaAvailable = AreaSku::getAvailable($pCycle,$seaDay);
        $ssd = self::query()->where('area_id',$areaId)->where('classify_id',$classifyId)->first();
        if($ssd){
            //更新
            $ssd->purchase_cycle = $pCycle;
            $ssd->sea_day = $seaDay;
            $ssd->sea_available = $seaAvailable;
            $ssd->status = self::setDateLe2Date($ssd->set_date,$seaAvailable);
            $ssd->save();
        }else{
            //创建
            self::query()->create([
                'area_id' => $areaId,
                'classify_id' => $classifyId,
                'purchase_cycle' => $pCycle,
                'sea_day' => $seaDay,
                'sea_available' => $seaAvailable,
                'set_date' => self::get2Date($seaAvailable),
                'status' => self::STATUS_NORMAL
            ]);
        }
    }

    /**
     * 获取周期
     * @param $countryIds
     * @param $classifyId
     * @return float|int
     */
    public static function getPCycle($countryIds,$classifyId){
        $pSkuIds = ProductAmazonSku::query()
            ->where('status',ProductAmazonSku::STATUS_YES) //已启用
            ->whereIn('country_id',$countryIds)
            ->whereHas('sku',function ($query) use ($classifyId){
                $query->where('classify_id',$classifyId);
            })->pluck('psku_id')
            ->toArray();
        if(empty($pSkuIds)){
            $pSkuIds = ProductSku::query()
                ->where('classify_id',$classifyId)
                ->pluck('id')
                ->toArray();
        }
        $pSkuIds = $pSkuIds ? array_unique($pSkuIds) : [];
        $pCycle = 0;
        foreach ($pSkuIds as $pSkuId){
            $ps = ProductSku::getModelById($pSkuId);
            $pCycleTemp = ProductSku::getPuCycle($ps);
            if($pCycle < $pCycleTemp){
                $pCycle = $pCycleTemp;
            }
        }
        return $pCycle;
    }

    /**
     * 获取海运+两个月
     * @param $seaSaleDays
     * @return false|string
     */
    public static function get2Date($seaSaleDays){
        return StockingSku::get2Date($seaSaleDays);
    }

    /**
     * 预警是否正常 当指定月 < 海运+2月 为0即异常
     * @param $setDate
     * @param $seaSaleDays
     * @return int
     */
    public static function setDateLe2Date($setDate,$seaSaleDays){
        if(!$setDate)return 0;
        $sea2Date = self::get2Date($seaSaleDays);
        return strtotime($setDate) < strtotime($sea2Date) ? 0 : 1;
    }

    /**
     * 获取设置的备货日期
     * @param $areaId
     * @param $classifyId
     * @param $seaSaleDays
     * @return false|mixed|string
     */
    public static function getSetDate($areaId,$classifyId,$seaSaleDays){
        $setDate = self::query()
            ->where('area_id',$areaId)
            ->where('classify_id',$classifyId)
            ->value('set_date');
        if($setDate){
            return $setDate;
        }else{
            return self::get2Date($seaSaleDays);
        }
    }

    /**
     * 获取备货日期模型/字段
     * @param $areaId
     * @param $classifyId
     * @param string $field
     * @return \Illuminate\Database\Eloquent\Builder[]|\Illuminate\Database\Eloquent\Collection|mixed
     */
    public static function getModel($areaId, $classifyId, $field = ''){
        $query = self::query();
        $query->where('area_id', $areaId);
        $query->where('classify_id', $classifyId);

        if ($field) {
            $res = $query->value($field);
        }else{
            $res = $query->first();
        }

        return $res;
    }

}
